﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using Ecreate.Popedom;
namespace Ecreate.Web
{
    public enum ActionType
    {
        add=0,
        edit=1,
        delete=2
    }

    public enum ConstType
    {
        action=0,
        id=1,
    }


    public class GridHelper
    {
        private EventHandler _deleteHandler = null;

        public EventHandler DeleteHandler
        {
            get { return _deleteHandler; }
            set { _deleteHandler = value; }
        }



        private string _crudUrl = "";

        public string CrudUrl
        {
            get { return _crudUrl; }
            set { _crudUrl = value; }
        }

        public  void setGridColumn(System.Web.UI.WebControls.GridView gridView, string field,string caption)
        {
            gridView.EmptyDataText = "<a href='" + CrudUrl + "?action=" + ActionType.add.ToString() + "' target='contentIframe'>添加</a>";

            gridView.Columns.Clear();
            gridView.AutoGenerateColumns = false;
            string[] CaptionArray = caption.Split(',');
            string[] FieldArray = field.Split(',');
            for (int i = 0; i < CaptionArray.Length; i++)
            {
                BoundField f = new BoundField();
                f.DataField = FieldArray[i];
                f.HeaderText = CaptionArray[i];
                f.HeaderStyle.ForeColor = System.Drawing.Color.White;
                gridView.Columns.Add(f);
            }

            if (this.DeleteHandler != null)
            {
                TemplateField tf = new TemplateField();
                tf.HeaderStyle.ForeColor = System.Drawing.Color.White;
                tf.HeaderTemplate = new utility.GridViewTemplateTextBox(DataControlRowType.Header, "删除", "Id");
                tf.ItemTemplate = new utility.GridViewTemplateTextBox(DataControlRowType.DataRow, "", "Id", this.DeleteHandler);
                gridView.Columns.Add(tf);
            }
            else
            {
                HyperLinkField deleteField = new HyperLinkField();
                deleteField.DataNavigateUrlFormatString = CrudUrl + "?action=" + ActionType.delete.ToString() + "&Id={0}";
                deleteField.DataNavigateUrlFields = new string[1] { "Id" };
                deleteField.HeaderText = "删除";
                deleteField.Text = "删除";
                deleteField.HeaderStyle.ForeColor = System.Drawing.Color.White;
                gridView.Columns.Add(deleteField);
            }
            HyperLinkField edtField = new HyperLinkField();
            edtField.DataNavigateUrlFormatString = CrudUrl + "?action=" + ActionType.edit.ToString()+"&Id={0}";
            edtField.DataNavigateUrlFields = new string[1] { "Id" };
            edtField.HeaderText = "编辑";
            edtField.Text = "编辑";
            edtField.HeaderStyle.ForeColor = System.Drawing.Color.White;


            gridView.Columns.Add(edtField);

            gridView.ShowFooter = true;
            gridView.RowDataBound += new GridViewRowEventHandler(gridView_RowDataBound);
        }









        public void setGridColumn(System.Web.UI.WebControls.GridView gridView, string field, string caption,EventHandler[] handlerArray)
        {
            gridView.EmptyDataText= "<a href='" + CrudUrl+"?action="+ActionType.add.ToString() + "' target='contentIframe'>添加</a>";


            gridView.Columns.Clear();
            gridView.AutoGenerateColumns = false;
            string[] CaptionArray = caption.Split(',');
            string[] FieldArray = field.Split(',');
            for (int i = 0; i < CaptionArray.Length; i++)
            {
                string fieldname = FieldArray[i];
                int pos = fieldname.IndexOf('.');
                if (pos > 0)
                {
                    TemplateField tf = new TemplateField();
                    tf.HeaderStyle.ForeColor = System.Drawing.Color.White;
                    tf.HeaderTemplate = new utility.GridViewTemplateLabel(DataControlRowType.Header, CaptionArray[i], FieldArray[i],null);
                    tf.ItemTemplate = new utility.GridViewTemplateLabel(DataControlRowType.DataRow, "", FieldArray[i], handlerArray[i]);
                    gridView.Columns.Add(tf);

                }
                else
                {
                    BoundField f = new BoundField();
                    f.DataField = FieldArray[i];
                    f.HeaderText = CaptionArray[i];
                    f.HeaderStyle.ForeColor = System.Drawing.Color.White;
                    gridView.Columns.Add(f);
                }
            }

            if (this.DeleteHandler != null)
            {
                TemplateField tf = new TemplateField();
                tf.HeaderStyle.ForeColor = System.Drawing.Color.White;
                tf.HeaderTemplate = new utility.GridViewTemplateTextBox(DataControlRowType.Header, "删除", "Id");
                tf.ItemTemplate = new utility.GridViewTemplateTextBox(DataControlRowType.DataRow, "", "Id", this.DeleteHandler);
                gridView.Columns.Add(tf);
            }
            else
            {
                HyperLinkField deleteField = new HyperLinkField();
                deleteField.DataNavigateUrlFormatString = CrudUrl + "?action=" + ActionType.delete.ToString() + "&Id={0}";
                deleteField.DataNavigateUrlFields = new string[1] { "Id" };
                deleteField.HeaderText = "删除";
                deleteField.Text = "删除";
                deleteField.HeaderStyle.ForeColor = System.Drawing.Color.White;
                gridView.Columns.Add(deleteField);
            }
            HyperLinkField edtField = new HyperLinkField();
            edtField.DataNavigateUrlFormatString = CrudUrl + "?action=" + ActionType.edit.ToString() + "&Id={0}";
            edtField.DataNavigateUrlFields = new string[1] { "Id" };
            edtField.HeaderText = "编辑";
            edtField.Text = "编辑";
            edtField.HeaderStyle.ForeColor = System.Drawing.Color.White;


            gridView.Columns.Add(edtField);

            gridView.ShowFooter = true;
            gridView.RowDataBound += new GridViewRowEventHandler(gridView_RowDataBound);
        }







        public string getDeleteFormatUrl()
        {
            string value = "<a href='" + this.CrudUrl + "?action=" + ActionType.delete.ToString() + "&id={0}' onclick='{1}'>" + "删除" + "</a>";
            return value;
        }



        void  gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "";
                string value = "<a href='" + CrudUrl+"?action="+ActionType.add.ToString() + "' target='contentIframe'>添加</a>";
                e.Row.Cells[(sender as GridView).Columns.Count - 1].Text = value;
            }
        }



    }


    public class GridHelperSearch
    {
        private string _crudUrl = "";

        public string CrudUrl
        {
            get { return _crudUrl; }
            set { _crudUrl = value; }
        }

        public void setGridColumn(System.Web.UI.WebControls.GridView gridView, string field, string caption, EventHandler[] handlerArray)
        {
            gridView.Columns.Clear();
            gridView.AutoGenerateColumns = false;
            string[] CaptionArray = caption.Split(',');
            string[] FieldArray = field.Split(',');
            for (int i = 0; i < CaptionArray.Length; i++)
            {
                string fieldname = FieldArray[i];
                int pos = fieldname.IndexOf('.');
                if (pos > 0)
                {
                    TemplateField tf = new TemplateField();
                    tf.HeaderStyle.ForeColor = System.Drawing.Color.White;
                    tf.HeaderTemplate = new utility.GridViewTemplateLabel(DataControlRowType.Header, CaptionArray[i], FieldArray[i], null);
                    tf.ItemTemplate = new utility.GridViewTemplateLabel(DataControlRowType.DataRow,"", FieldArray[i], handlerArray[i]);
                    gridView.Columns.Add(tf);

                }
                else
                {
                    BoundField f = new BoundField();
                    f.DataField = FieldArray[i];
                    f.HeaderText = CaptionArray[i];
                    f.HeaderStyle.ForeColor = System.Drawing.Color.White;
                    gridView.Columns.Add(f);
                }
            }
        }



        public void setGridColumn(System.Web.UI.WebControls.GridView gridView, string field, string caption)
        {
            gridView.EmptyDataText = "查询结果为空";

            gridView.Columns.Clear();
            gridView.AutoGenerateColumns = false;
            string[] CaptionArray = caption.Split(',');
            string[] FieldArray = field.Split(',');
            for (int i = 0; i < CaptionArray.Length; i++)
            {
                string fieldname = FieldArray[i];
                BoundField f = new BoundField();
                f.DataField = FieldArray[i];
                f.HeaderText = CaptionArray[i];
                f.HeaderStyle.ForeColor = System.Drawing.Color.White;
                gridView.Columns.Add(f);
            }
        }

    }
}
